package com.jibei.mendo.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jibei.mendo.R;

import java.util.List;

/**
 * Created by lhl on 2019/9/1.
 * 检测页面中的设备适配器
 */
public class CheckPortAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<String> strings;

    public CheckPortAdapter(Context context, List<String> strings) {
        this.mContext = context;
        this.strings = strings;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_check_port, viewGroup, false);
        return new CheckPortAdapter.FileViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        String str = String.valueOf(position + 1);
        if (str.length() == 1) {
            str = "0" + str;
        }
        ((CheckPortAdapter.FileViewHolder) viewHolder).mText.setText(str);

        String stringBg = strings.get(position);
        if (stringBg.equals("0")) {
            ((CheckPortAdapter.FileViewHolder) viewHolder).mText.setBackgroundColor(mContext.getResources().getColor(R.color.red));
        }
        if (stringBg.equals("1")) {
            ((CheckPortAdapter.FileViewHolder) viewHolder).mText.setBackgroundColor(mContext.getResources().getColor(R.color.blue));
            //((CheckPortAdapter.FileViewHolder) viewHolder).mText.setTextColor(mContext.getResources().getColor(R.color.white_color));
        }
        if (stringBg.equals("2")) {
            ((CheckPortAdapter.FileViewHolder) viewHolder).mText.setBackgroundColor(mContext.getResources().getColor(R.color.yellow));
            //((CheckPortAdapter.FileViewHolder) viewHolder).mText.setTextColor(mContext.getResources().getColor(R.color.white_color));
        }
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    private class FileViewHolder extends RecyclerView.ViewHolder {

        private TextView mText;

        public FileViewHolder(@NonNull View itemView) {
            super(itemView);
            mText = itemView.findViewById(R.id.tv_check_port_name);
        }
    }
}
